package com.aaa.ssm.service.impl;

import com.aaa.ssm.dao.DeptDao;
import com.aaa.ssm.entity.Dept;
import com.aaa.ssm.service.DeptService;
import com.aaa.ssm.util.CustomException;
import com.aaa.ssm.util.Page;
import com.aaa.ssm.util.Result;
import com.aaa.ssm.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

/**
 * @FileName: DeptServiceImpl
 */
@Service
public class DeptServiceImpl  implements DeptService {
    @Resource
    @Autowired
    private DeptDao deptDao;
    @Override
    public int add(Dept dept) {
        return deptDao.add(dept);
    }

    @Override
    public int update(Dept dept) {
        return deptDao.update(dept);
    }

    @Override
    public int deleteById(Integer deptNo) {
        return deptDao.deleteById(deptNo);
    }

    @Override
    public Dept queryById(Integer deptNo) {
        if (deptNo == null || deptNo <= 0) {
            throw new CustomException(ResultStatus.SUCCESS.getStatusCode() , ResultStatus.FAIL.getStatusMessage());
        }
        return deptDao.queryById(deptNo);
    }
    @Override
    public PageInfo queryByPage(Page<Dept> page) {
        // 获取
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        // 判断pageNo和pageSize不能为空和0
        if (pageNo==null ||pageNo==0||pageSize==null ||pageSize==0){
            // 抛出异常，代码终止
            throw new CustomException(ResultStatus.SUCCESS.getStatusCode(), ResultStatus.FAIL.getStatusMessage());
        }
        //调用分页插件中的方法，传入页码和每一页显示数量，底层开始分页
        PageHelper.startPage(pageNo,pageSize);
        // 获取查询参数
        Dept data = page.getData();
        // 调用dao层查询，并且实例化PageInfo返回结果
        return new PageInfo(deptDao.queryByParam(data));
    }

//    @Override
//    public List<Dept> queryByParam(Dept dept) {
//        return deptDao.queryByParam(dept);
//    }
    }
